<?php 

namespace app\recordpen\controller;

use app\recordpen\controller\FileTagLogic;

class FileTag {
	protected static $Request;

	function __construct() {
		self::$Request = request()->param();
	}

	/**
	 * 创建文件标注
	 */
	public function createFileTag() {
		try {
			$fid = self::$Request['fid'];
			$tag = json_encode(json_decode(self::$Request['tag'], true));
			$res = FileTagLogic::createFileTagLogic($fid, $tag);
			return JRes($res);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	/**
	 * 获取文件标注
	 */
	public function getFileTag() {
		try {
			$fid = self::$Request['fid'];
			$res = FileTagLogic::getFileTagLogic($fid);
			$returnData = [
				'tag' => isset($res->data->tag) ? $res->data->tag : '[]'
			];
			return resultArray(['data' => $returnData]);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	/**
	 * 新增文件标注
	 */
	public function addFileTag() {
		try {
			$fid = self::$Request['fid'];
			$keyArr = json_decode(self::$Request['key_arr'], true);
			$res = FileTagLogic::addFileTagLogic($fid, $keyArr);
			return JRes($res);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	/**
	 * 删除文件标注
	 */
	public function deleteFileTag() {
		try {
			$fid = self::$Request['fid'];
			$deleteArr = json_decode(self::$Request['delete_arr'], true);
			$res = FileTagLogic::deleteFileTagLogic($fid, $deleteArr);
			if ($res->isSuccess) {
				return resultArray(['data' => $res->data]);
			}
			return resultArray(['error' => $res->info]);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}
}